import Vue from 'vue'
import VueRouter from 'vue-router'
import mainRouter from '@/views/mian/mainRouter'

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'hasbottomnav',
    redirect:'/home',
    component: () => import ('../views/mian/hasBottomNav'),
    children:[
      ...mainRouter
    ]
  },
  {
    path: '/classgoods',
    name: 'classgoods',
    component: () => import( '../views/classGoods.vue')
  },
  {
    path:'/login',
    name:'login',
    component:() => import('../views/login')
  },{
    path:'/detail',
    name:'detail',
    component:() => import('../views/detail')
  },{
    path:"/cutprice",
    name:'cutprice',
    component:() => import('../views/Cutprice')
  },{
    path:"/special",
    name:"special",
    component:() => import('../views/special')
  },
  {
    path:"/topay",
    name:'topay',
    component :() => import ('../views/topay'),
    
  }
]

const router = new VueRouter({
  routes
})


const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  if(typeof(location)=="string"){
    var Separator = "&";
    if(location.indexOf('?')==-1) { Separator='?'; }
    location = location + Separator + "random=" + Math.random();
  }
  return routerPush.call(this, location).catch(error=> error)
}

// router.beforEach((to,from ,next)=> {
//   if(to.name === "topay"){
//       if(this.$store.state.token !== ""){
//           next()
//       }else{
//           next('/login')
//       }
//   }
// })

router.beforeEach((to, from, next) => {
    if(to.name == "topay"){
        if(localStorage.getItem('token')){
          next()
        }else{
          next('/login')
        }
    }else{
      next()
    }
})




export default router
